<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Students extends Backend_Controller {

	protected $_title = 'Siswa';

	public function __construct() {
		parent::__construct();
		if(parent::get_actor_login()!="admin")
			redirect('login/index/1');
		$this->load->model('classes_model', 'classes', true);
		//$this->load->model('users_model', 'users', true);
	}
		
	public function index($offset=0, $sort_by='name') {
		
		$data = array(
			'mainview' => 'index',
		) + $this->_get_header_data();
		
		$this->load->view('admin/template',$data);
		
	}
	
	function _crud_output($output = null) {
        $this->load->view('admin/template_crud.php',$output);    
    }

	function students_display(){	
		$crud = new grocery_CRUD();
        $crud->set_model('student_user');
		$crud->set_theme('flexigrid');
        $crud->set_table('classes');
        $crud->set_subject('Siswa');

		$crud->columns('user_id','username','nis','studentname','name','absent');
		$crud->display_as('user_id','User ID');
		$crud->display_as('nis','No Induk');
		$crud->display_as('studentname','Nama');
		$crud->display_as('name','Kelas');
		$crud->display_as('absent','No Absent');
		$crud->required_fields('nis','studentname','name','absent','username');
		$crud->change_field_type('user_id','invisible');
		$crud->change_field_type('password','password');
		$crud->callback_edit_field('password',array($this,'callback_edit_password'));
		$data = $this->classes->lists_array();
		
		$crud->change_field_type('name','enum',$data);
		$crud->fields('user_id','nis','studentname','name','absent','username','password');
		
		//$crud->set_rules('name', 'name','callback_student_check');

        $output = $crud->render();
        $this->_crud_output($output);	
	}
	
	function insert_user_id($post_array,$primary_key)
	{
		$lastid = $this->users->insert($post_array);
		$post_array['school_year_id'] = $this->classes->get_current_school_year_id();
		return $post_array;
	}
	
	function callback_edit_password(){
		return '<input type="password" maxlength="50" value="" name="password" style="width:400px"> ( biarkan kosong = tidak mengganti password )';
	}
}
